package com.scalar.chiptrack.finance.reports;

import java.util.Comparator;
import java.io.Serializable;

/**
 * User: Sujan
 * Date: July 10, 2007
 * Time: 1:25:27 PM
 */
public class DirectMarginComparator implements Comparator, Serializable
{
    private String sortBy;
    private String sortByKey;
    private String orderBy;
    public DirectMarginComparator ( String sortBy, String sortByKey, String orderBy )
    {
        this.sortBy = sortBy;
        this.sortByKey = sortByKey;
        this.orderBy = orderBy;
    }
    public int compare( Object directMarging1, Object directMargin2 )
    {
        if ( !( directMarging1 instanceof DirectMargin) || !( directMargin2 instanceof DirectMargin ) )
        {
            throw new  ClassCastException ( );
        }
        float order1 = 0;
        float order2 = 0;
        int result = 0;
        if ( "value".equalsIgnoreCase( sortBy ) )
        {
            order1 = ( ( DirectMargin )directMarging1 ).getDirectMargin();
            order2 = ( ( DirectMargin )directMargin2).getDirectMargin();
            result =  ( order1 < order2 ? -1 :( order1 == order2 ? 0 : 1) );            
        }
        else
        {
            if ( "customer".equalsIgnoreCase( sortByKey ) )
            {
                result = ( ( DirectMargin )directMarging1 ).getCustomerName().compareToIgnoreCase(( ( DirectMargin )directMargin2 ).getCustomerName() );
            }
            else if ( "product".equalsIgnoreCase( sortByKey ) )
            {
                result = ( ( DirectMargin )directMarging1 ).getPartNumber().compareToIgnoreCase(( ( DirectMargin )directMargin2 ).getPartNumber() );
            }
            else if ( "costCode".equalsIgnoreCase( sortByKey ) )
            {
                result = ( ( DirectMargin )directMarging1 ).getCostCode().compareToIgnoreCase(( ( DirectMargin )directMargin2 ).getCostCode() );
            }
            else if ( "salesOrder".equalsIgnoreCase( sortByKey ) )
            {
                result = ( ( DirectMargin )directMarging1 ).getSalesOrderNo().compareToIgnoreCase(( ( DirectMargin )directMargin2 ).getSalesOrderNo() );
            }
        }
        if ( result != 0 && "descending".equalsIgnoreCase( orderBy ) )
        {
            result = result * -1; //descending order
        }

        return result;
    }
}
